/*
 * net.h
 *
 *  Created on: Apr 3, 2014
 *      Author: killerg
 */

#ifndef NET_H_
#define NET_H_

#include <stddef.h>
#include <stdint.h>
struct event_base;
struct evdns_base;
struct bufferevent;
struct listener_t;
struct connector_t;

struct conn_t {
	union {
		listener_t* listener;
		connector_t* connector = NULL;
	};
	bufferevent* bev = NULL;
	void* cookie = NULL;
	int shutting:1,
		eof:1;
};

typedef void (*conn_accept_cb_t)(conn_t* conn);
typedef void (*conn_connect_cb_t)(conn_t* conn);
typedef void (*conn_disconnect_cb_t)(conn_t* conn);
typedef void (*conn_data_cb_t)(conn_t* conn);



template<typename MsgT>
extern bool conn_send(conn_t* conn, MsgT* &msg);
extern size_t conn_send(conn_t* conn, void* msg, size_t size);
extern void conn_disconnect(conn_t* conn);

extern listener_t* start_listen(
		event_base* base,
		const char* ip,
		const uint16_t port,
		conn_accept_cb_t acceptcb,
		conn_disconnect_cb_t disconnectcb,
		conn_data_cb_t datacb);
extern void stop_listen(listener_t* listener);
extern conn_t* start_connect(
		event_base* base,
		evdns_base* dns,
		const char* addr,
		short port,
		conn_connect_cb_t connectcb,
		conn_disconnect_cb_t disconnectcb,
		conn_data_cb_t datacb);


#endif /* NET_H_ */
